﻿using System;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;
using Diploma.Validation;

namespace DiplomaWeb.Controllers
{
    [Authorize]
    public class CollaborationRoomsController : MasterController
    {
        public CollaborationRoomsController(DataManager dataManager)
            : base(dataManager)
        {
        }

        [HttpGet]
        [Authorize(Roles = "Lecturer")]
        public ActionResult Add()
        {
            ViewData["Lecturers"] = new SelectList(DataManager.LecturersRepository.GetConsultants(LoggedUser.UserId),
                                                   "UserId", "FullName", null);

            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Lecturer")]
        public ActionResult Add(CollaborationRoom room)
        {
            ValidationErrors = Validator.ValidateCollaborationRoom(room);
            if (ValidationErrors.Count() == 0)
            {
                room.Lecturer = LoggedUser as Lecturer;
                DataManager.CollaborationRoomRepository.Add(room);
                return RedirectToRoute("Default", new { controller = "Lecturers", action = "Themes", });
            }
            else
            {
                foreach (ValidationError validationError in ValidationErrors)
                {
                    ModelState.AddModelError(validationError.FieldName, validationError.Message);
                }

                ViewData["Lecturers"] = new SelectList(
                    DataManager.LecturersRepository.GetConsultants(LoggedUser.UserId), "UserId", "FullName", null);
                return View(room);
            }
        }

        [HttpGet]
        [Authorize(Roles = "Lecturer")]
        public ActionResult Edit(Guid id)
        {
            CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(id);
            ViewData.Model = room;
            ViewData["Lecturers"] = new SelectList(DataManager.LecturersRepository.GetConsultants(LoggedUser.UserId),
                                                   "UserId", "FullName", room.Consultant);

            return View();
        }


        [HttpPost]
        [Authorize(Roles = "Lecturer")]
        public ActionResult Edit(CollaborationRoom room)
        {
            ValidationErrors = Validator.ValidateCollaborationRoom(room);
            if (ValidationErrors.Count() == 0)
            {
                DataManager.CollaborationRoomRepository.Edit(room);
                return RedirectToRoute("Default", new { controller = "Lecturers", action = "Themes", });
            }
            else
            {
                foreach (ValidationError validationError in ValidationErrors)
                {
                    ModelState.AddModelError(validationError.FieldName, validationError.Message);
                }

                ViewData.Model = room;
                ViewData["Lecturers"] = new SelectList(
                    DataManager.LecturersRepository.GetConsultants(LoggedUser.UserId), "UserId", "FullName",
                    room.Consultant);

                return View();
            }
        }

        [Authorize(Roles = "Lecturer")]
        public ActionResult Delete(Guid id)
        {
            DataManager.CollaborationRoomRepository.Delete(id);

            return RedirectToRoute("Default", new { controller = "Lecturers", action = "Themes", });
        }

        [Authorize(Roles = "Student")]
        public ActionResult RefuseRoom()
        {
            var student = LoggedUser as Student;
            DataManager.CollaborationRoomRepository.Refuse(student);

            return Redirect(Request.UrlReferrer.OriginalString);
        }

        [Authorize(Roles = "Lecturer")]
        public ActionResult RefuseStudent(Guid studentId)
        {
            Student student = DataManager.StudentsRepository.GetStudent(studentId);
            DataManager.CollaborationRoomRepository.Refuse(student);
            return Redirect(Request.UrlReferrer.OriginalString);
        }

        [Authorize(Roles = "Lecturer")]
        public ActionResult Approve(Guid roomId, Guid studentId)
        {
            CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(roomId);
            if (room.Lecturer.UserId != room.LecturerId)
            {
                throw new Exception("It's not your student");
            }
            Student student = DataManager.StudentsRepository.GetStudent(studentId);

            DataManager.CollaborationRoomRepository.Approve(room, student);

            return Redirect(Request.UrlReferrer.OriginalString);
        }

        public ActionResult Enter(Guid id)
        {
            //TODO(vbarashkov): Please delete NotImplementedException() and write correct logic
            CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(id);
            if (LoggedUser is Student)
            {
                if (room.Student.UserId == LoggedUser.UserId)
                {
                    return RedirectToRoute("Default", new { controller = "Students", action = "Home", });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (LoggedUser is Lecturer)
            {
                if (room.Lecturer.UserId == LoggedUser.UserId)
                    return RedirectToRoute("Default",
                                           new
                                               {
                                                   controller = "Lecturers",
                                                   action = "CollaborationRoom",
                                                   id = room.CollaborationRoomId
                                               });
                else if (room.ConsultantId == LoggedUser.UserId)
                {
                    return RedirectToRoute("Default",
                                           new
                                               {
                                                   controller = "Lecturers",
                                                   action = "CollaborationRoom",
                                                   id = room.CollaborationRoomId
                                               });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (LoggedUser is SystemManager)
            {
                throw new NotImplementedException();
            }

            throw new NotImplementedException();
        }

        public ActionResult Proposes(Guid id)
        {

            CollaborationRoom room = DataManager.CollaborationRoomRepository.GetCollaborationRoom(id);
            if (room == null)
                throw new Exception();
            ViewData.Model = room;
            return PartialView("~/Views/Lecturers/StudentsProposesControl.ascx");
        }
    }
}